package cn.powerone.scm.service.impl;

import java.util.List;
import cn.powerone.common.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmSequenceParmMapper;
import cn.powerone.scm.domain.ScmSequenceParm;
import cn.powerone.scm.service.IScmSequenceParmService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 序号参数Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-08-05
 */
@Service
public class ScmSequenceParmServiceImpl implements IScmSequenceParmService 
{
    @Autowired
    private ScmSequenceParmMapper scmSequenceParmMapper;

    /**
     * 查询序号参数
     * 
     * @param id 序号参数主键
     * @return 序号参数
     */
    @Override
    public ScmSequenceParm selectScmSequenceParmById(Long id)
    {
        return scmSequenceParmMapper.selectScmSequenceParmById(id);
    }

    /**
     * 查询序号参数列表
     * 
     * @param scmSequenceParm 序号参数
     * @return 序号参数
     */
    @Override
    public List<ScmSequenceParm> selectScmSequenceParmList(ScmSequenceParm scmSequenceParm)
    {
        return scmSequenceParmMapper.selectScmSequenceParmList(scmSequenceParm);
    }

    /**
     * 新增序号参数
     * 
     * @param scmSequenceParm 序号参数
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScmSequenceParm(ScmSequenceParm scmSequenceParm)
    {
        scmSequenceParm.setCreateTime(DateUtils.getNowDate());
        return scmSequenceParmMapper.insertScmSequenceParm(scmSequenceParm);
    }

    /**
     * 修改序号参数
     * 
     * @param scmSequenceParm 序号参数
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScmSequenceParm(ScmSequenceParm scmSequenceParm)
    {
        scmSequenceParm.setUpdateTime(DateUtils.getNowDate());
        return scmSequenceParmMapper.updateScmSequenceParm(scmSequenceParm);
    }

    /**
     * 批量删除序号参数
     * 
     * @param ids 需要删除的序号参数主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmSequenceParmByIds(Long[] ids)
    {
        return scmSequenceParmMapper.deleteScmSequenceParmByIds(ids);
    }

    /**
     * 删除序号参数信息
     * 
     * @param id 序号参数主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmSequenceParmById(Long id)
    {
        return scmSequenceParmMapper.deleteScmSequenceParmById(id);
    }

    /**
     * 生成序号
     * @param seqType
     * @return
     */
    @Override
    @Transactional
    public String generateScmSequence(String seqType) {
        ScmSequenceParm scmSequenceParm = scmSequenceParmMapper.selectScmSequenceParmByType(seqType);

        // 获取当前日期
        String currentDate = DateUtils.dateTimeNow("yyyyMMdd");

        // 使用配置的格式生成序号
        // 根据格式字符串中%d和%s的位置决定参数顺序
        String orderNumberFormat = scmSequenceParm.getOrderNumberFormat();
        long seqNumber = scmSequenceParm.getSeqNumber() + 1; //自增序号

        // 检查%d和%s的位置
        if (orderNumberFormat != null) {
            int dIndex = 0;
            int sIndex = orderNumberFormat.indexOf("%s");
            int maxDigits = 0 ;
            // 使用正则表达式匹配 % 后面跟着的数字格式
            java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("%0?(\\d*)d");
            java.util.regex.Matcher matcher = pattern.matcher(orderNumberFormat);
            if (matcher.find()) {
                dIndex = matcher.start();
                String width = matcher.group(1);
                if (!width.isEmpty()) {
                    maxDigits = Integer.parseInt(width);
                }
            }

            // 计算数字的位数
            int digitCount = String.valueOf(Math.abs(seqNumber)).length();

            // 如果数字位数超过指定最大位数，则重置为1
            if (digitCount > maxDigits) {
                seqNumber = 1;
            }
            // 保存新序号
            scmSequenceParm.setSeqNumber(seqNumber);
            scmSequenceParmMapper.updateScmSequenceParm(scmSequenceParm);

            // 如果%d在%s之后，则先传currentDate，再传seqNumber
            if (dIndex > sIndex) {
                return String.format(orderNumberFormat, currentDate, seqNumber);
            } else {
                // 如果%d在%s之前，则先传seqNumber，再传currentDate
                return String.format(orderNumberFormat, seqNumber, currentDate);
            }
        }
        return null;
    }

    @Override
    @Transactional
    public void resetAllSeqToOne() {
        scmSequenceParmMapper.resetAllSeqToOne();
    }
}
